library(foreign)
library(ggplot2)
library(scales)
rm(list=ls())

####################################################################
# Figure A.3: Predicted Turnout Probabilities in Referendums 
# with and without Postal Voting, 1981-2009 (Individual-level Data)
####################################################################

data <- read.dta("./marginal_effects/me_postal_all.dta")


label_group=data$group_description[data$indi_group_descr==1]
breaks_group=data$id[data$indi_group_descr==1]


label_group_members=data$group_member_descr[data$indi_group_member_descr==1]
breaks_group_members=data$id[data$indi_group_member_descr==1]

data$variable_num <- factor(data$variable_num,
                            labels = label_group)

data$id <- data$id-0.5



ggplot(data, aes(y = id, x = beta, shape=factor(postal),xmin = beta - 1.96*sqrt(beta_cov), xmax=beta + 1.96* sqrt(beta_cov))) +
  geom_point(position= "dodge",size=4.8) + 
  geom_errorbarh(position= "dodge",height=0.8) +
  facet_grid(variable_num ~ ., scales = "free", space = "free") +
  theme_bw(base_size = 32)  + 
  ylab("") + xlab("Turnout Probability") +
  theme(strip.text.y = element_text(angle=0)) +
  scale_x_continuous(limits = c(0, 1)) +
  scale_y_continuous(expand = c(0, 0.6),breaks=breaks_group_members,labels=label_group_members) +
  scale_shape_manual(values=c(1,16),name = "",labels = c("No Postal Voting  ","Postal Voting")) +
  theme(legend.direction = "horizontal", legend.position = "bottom") +
  theme(panel.grid.minor.y =  element_line(colour = "white", size = 0.5,linetype="dotted")) +
  theme(legend.key = element_blank(), strip.background = element_rect(colour="black", fill="grey90" ) ) + 
  theme(panel.margin = unit(0.8, "lines"))+ 
  theme(strip.background = element_blank(), strip.text = element_blank()) +
  theme(axis.text.y = element_text(face=data$title))


ggsave(file="FigureA3.pdf",width=15,height=18 )
